<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

if($sys->get['ope'] == "topic_add"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['topic_id'])){
        $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['topic_id_add_error']."<!-- topic_id_add_error -->");
    }else{
        
        $result_topic       = func::db_select(DB_TABLE_TOPIC, array("id"), "id=".(int)$sys->get['topic_id']);
        $result_topic       = $result_topic[0];
        $result_favorite    = func::db_select(DB_TABLE_MEMBER_FAVORITE, array("mid"), "mid=".$MEMBER['id']." AND theid=".$result_topic['id']);
        $result_favorite    = $result_favorite[0];
        if(empty($result_topic)){
            $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['topic_not_exist']."<!-- topic_not_exist -->");
        }elseif(!empty($result_favorite)){
            $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['topic_already_exist']."<!-- topic_already_exist -->");
        }else{
             // insert
             $sql = "INSERT ".DB_TABLE_MEMBER_FAVORITE." SET
            			mid                 =".$MEMBER['id'].",
                        type                ='topic',
                        theid               =".$result_topic['id'].",
                        time_created        =".$sys->nowtime.",
                        rank                =0";
             $result_friend = $db->Execute($sql);            
             if (!$result_friend) {
                 echo $db->ErrorMsg();
             }
             $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite");
        }
    }

}elseif($sys->get['ope'] == "topic_del"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['topic_id'])){
        $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['topic_id_del_error']."<!-- topic_id_del_error -->");
    }else{
        // delete
        $sql = "DELETE FROM ".DB_TABLE_MEMBER_FAVORITE." WHERE 
                    mid = ".$MEMBER['id']." AND theid = '".$sys->get['topic_id']."'";
        $result = $db->Execute($sql);            
        if (!$result) {
            echo $db->ErrorMsg();
        }
    }
    $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite&pag=".$sys->get['pag']);

}elseif($sys->get['ope'] == "topic_update"){

    foreach($sys->post['rank'] as $key => $value){
         // update
         $sql = "UPDATE ".DB_TABLE_MEMBER_FAVORITE." SET
                     rank = '".(int)addslashes($value)."'
                 WHERE mid = ".$MEMBER['id']." AND type='topic' AND theid=".(int)$key."";
         $result = $db->Execute($sql);            
         if (!$result) {
             echo $db->ErrorMsg();
         }
    }
    foreach($sys->post['del'] as $key => $value){
         // delete
         $sql = "DELETE FROM ".DB_TABLE_MEMBER_FAVORITE." 
                 WHERE mid = ".$MEMBER['id']." AND type='topic' AND theid=".(int)$key."";
         $result = $db->Execute($sql);            
         if (!$result) {
             echo $db->ErrorMsg();
         }
    }
    $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite");

}elseif($sys->get['ope'] == "channel_add"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['channel_id'])){
        $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['channel_id_add_error']."<!-- mid error -->");
    }else{
        
        $result_channel       = func::db_select(DB_TABLE_CHANNEL, array("id"), "id=".(int)$sys->get['channel_id']);
        $result_channel       = $result_channel[0];
        $result_favorite    = func::db_select(DB_TABLE_MEMBER_FAVORITE, array("mid"), "mid=".$MEMBER['id']." AND theid=".$result_channel['id']);
        $result_favorite    = $result_favorite[0];
        if(empty($result_channel)){
            $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['channel_not_exist']."<!-- channel_not_exist -->");
        }elseif(!empty($result_favorite)){
            $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['channel_already_exist']."<!-- channel_already_exist -->");
        }else{
             // insert
             $sql = "INSERT ".DB_TABLE_MEMBER_FAVORITE." SET
            			mid                 =".$MEMBER['id'].",
                        type                ='channel',
                        theid               =".$result_channel['id'].",
                        time_created        =".$sys->nowtime.",
                        rank                =0";
             $result_friend = $db->Execute($sql);            
             if (!$result_friend) {
                 echo $db->ErrorMsg();
             }
             $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite&ope=channel");
        }
    }

}elseif($sys->get['ope'] == "channel_del"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['channel_id'])){
        $sys->cp_prompt("failed",$LANGUAGE['s']['cp_favorite']['channel_id_del_error']."<!-- channel_id_del_error -->");
    }else{
        // delete
        $sql = "DELETE FROM ".DB_TABLE_MEMBER_FAVORITE." WHERE 
                    mid = ".$MEMBER['id']." AND theid = '".$sys->get['channel_id']."'";
        $result = $db->Execute($sql);            
        if (!$result) {
            echo $db->ErrorMsg();
        }
    }
    $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite&ope=channel&pag=".$sys->get['pag']);

}elseif($sys->get['ope'] == "channel_update"){

    foreach($sys->post['rank'] as $key => $value){
         // update
         $sql = "UPDATE ".DB_TABLE_MEMBER_FAVORITE." SET
                     rank = '".(int)addslashes($value)."'
                 WHERE mid = ".$MEMBER['id']." AND type='channel' AND theid=".(int)$key."";
         $result = $db->Execute($sql);            
         if (!$result) {
             echo $db->ErrorMsg();
         }
    }
    foreach($sys->post['del'] as $key => $value){
         // delete
         $sql = "DELETE FROM ".DB_TABLE_MEMBER_FAVORITE." 
                 WHERE mid = ".$MEMBER['id']." AND type='channel' AND theid=".(int)$key."";
         $result = $db->Execute($sql);            
         if (!$result) {
             echo $db->ErrorMsg();
         }
    }
    $sys->cp_prompt("jump",$CONFIGURE['common']['control_index']."?act=cp_favorite&ope=channel");

}elseif($sys->get['ope'] == "channel"){

    $sql_where_total    = "mid=".$MEMBER['id']." AND type='channel'";
    $sql_where          = "member_favorite.mid=".$MEMBER['id']." AND member_favorite.type='channel'";
    $total_record       = func::db_count_record(DB_TABLE_MEMBER_FAVORITE, $sql_where_total);
    if ($total_record > 0) {
        $bwpage->set_record_listnum($CONFIG['page_record_cp_favorite_channel']);
        $bwpage->set_record($total_record);
        $bwpage->page($CONFIGURE['common']['control_index']."?act=cp_favorite&ope=channel&pag=%s");
        $sql = "SELECT member_favorite.*, 
                    channel.id as channel_id, channel.name as channel_name, 
                    channel.number_total_topic as channel_number_total_topic, 
                    channel.number_total_post as channel_number_total_post, 
                    channel.number_today_topic as channel_number_today_topic, 
                    channel.number_today_post as channel_number_today_post,
                    channel.last_topic_id as channel_last_topic_id, 
                    channel.last_topic_subject as channel_last_topic_subject, 
                    channel.last_topic_mid as channel_last_topic_mid, 
                    channel.last_topic_musername as channel_last_topic_musername, 
                    channel.last_topic_time as channel_last_topic_time, 
                    channel.last_post_id as channel_last_post_id, 
                    channel.last_post_topic_id as channel_last_post_topic_id, 
                    channel.last_post_mid as channel_last_post_mid, 
                    channel.last_post_musername as channel_last_post_musername, 
                    channel.last_post_time as channel_last_post_time
                FROM ".DB_TABLE_MEMBER_FAVORITE." member_favorite
                    LEFT JOIN ".DB_TABLE_CHANNEL." channel ON channel.id=member_favorite.theid
                WHERE ".$sql_where."
                ORDER BY member_favorite.rank
                LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $favorite_channel_list[] = array(
                    'mid'                           => $result->fields['mid'],
                    'type'                          => $result->fields['type'],
                    'theid'                         => $result->fields['theid'],
                    'time_created'                  => $result->fields['time_created'],
                    'rank'                          => $result->fields['rank'],

                    'channel_id'                    => $result->fields['channel_id'],
                    'channel_name'                  => $result->fields['channel_name'],
                    'channel_number_total_topic'    => $result->fields['channel_number_total_topic'],
                    'channel_number_total_post'     => $result->fields['channel_number_total_post'],
                    'channel_number_today_topic'    => $result->fields['channel_number_today_topic'],
                    'channel_number_today_post'     => $result->fields['channel_number_today_post'],
                    'channel_last_topic_id'         => $result->fields['channel_last_topic_id'],
                    'channel_last_topic_subject'    => $result->fields['channel_last_topic_subject'],
                    'channel_last_topic_mid'        => $result->fields['channel_last_topic_mid'],
                    'channel_last_topic_musername'  => $result->fields['channel_last_topic_musername'],
                    'channel_last_topic_time'       => $result->fields['channel_last_topic_time'],
                    'channel_last_post_id'          => $result->fields['channel_last_post_id'],
                    'channel_last_post_topic_id'    => $result->fields['channel_last_post_topic_id'],
                    'channel_last_post_mid'         => $result->fields['channel_last_post_mid'],
                    'channel_last_post_musername'   => $result->fields['channel_last_post_musername'],
                    'channel_last_post_time'        => $result->fields['channel_last_post_time'],
                    );
                $result->MoveNext();
            }
        }

        $t->assign(array(
            "favorite_channel_list"   => $favorite_channel_list,
            "pagination"            => $bwpage->pagination,
        ));
    }
}else{

    $sql_where_total    = "mid=".$MEMBER['id']." AND type='topic'";
    $sql_where          = "member_favorite.mid=".$MEMBER['id']." AND member_favorite.type='topic'";
    $total_record       = func::db_count_record(DB_TABLE_MEMBER_FAVORITE, $sql_where_total);
    if ($total_record > 0) {
        $bwpage->set_record_listnum($CONFIG['page_record_cp_favorite_topic']);
        $bwpage->set_record($total_record);
        $bwpage->page($CONFIGURE['common']['control_index']."?act=cp_favorite&pag=%s");
        $sql = "SELECT member_favorite.*, 
                    topic.id as topic_id, topic.subject as topic_subject, topic.post_mid as topic_post_mid, topic.post_musername as topic_post_musername, 
                    topic.number_reply as topic_number_reply,
                    topic.number_click as topic_number_click,
                    topic.last_time as topic_last_time, 
                    topic.last_mid as topic_last_mid, 
                    topic.last_musername as topic_last_musername,
                    topic.last_pid as topic_last_pid,
                    channel.id as channel_id, channel.name as channel_name
                FROM ".DB_TABLE_MEMBER_FAVORITE." member_favorite
                    LEFT JOIN ".DB_TABLE_TOPIC." topic ON topic.id=member_favorite.theid
                    LEFT JOIN ".DB_TABLE_CHANNEL." channel ON channel.id=topic.channel_id
                WHERE ".$sql_where."
                ORDER BY member_favorite.rank
                LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $favorite_topic_list[] = array(
                    'mid'                   => $result->fields['mid'],
                    'type'                  => $result->fields['type'],
                    'theid'                 => $result->fields['theid'],
                    'time_created'          => $result->fields['time_created'],
                    'rank'                  => $result->fields['rank'],

                    'topic_id'              => $result->fields['topic_id'],
                    'topic_subject'         => $result->fields['topic_subject'],
                    'topic_post_mid'        => $result->fields['topic_post_mid'],
                    'topic_post_musername'  => $result->fields['topic_post_musername'],
                    'topic_number_reply'    => $result->fields['topic_number_reply'],
                    'topic_number_click'    => $result->fields['topic_number_click'],
                    'topic_last_time'       => $result->fields['topic_last_time'],
                    'topic_last_mid'        => $result->fields['topic_last_mid'],
                    'topic_last_musername'  => $result->fields['topic_last_musername'],
                    'topic_last_pid'        => $result->fields['topic_last_pid'],

                    'channel_id'            => $result->fields['channel_id'],
                    'channel_name'          => $result->fields['channel_name'],
                    );
                $result->MoveNext();
            }
        }

        $t->assign(array(
            "favorite_topic_list"   => $favorite_topic_list,
            "pagination"            => $bwpage->pagination,
        ));
    }
}
$public_var['page_place'][] = $LANGUAGE['s']['cp_favorite']['page_place_upper'];
$public_var['page_place'][] = $LANGUAGE['s']['cp_favorite']['page_place'];
?>
